unit WARCommons;

interface

uses DBXJSON, DBXJSONReflect, system.SysUtils, system.Classes, system.Variants,
  WARConfig, vcl.forms, superobject, IdHTTP, data.db;

type

  TURLQueueObject = class;

  TEmblem = record

  [SODefault('0')]
    icon: integer;
    [SODefault('0')]
    border: integer;
    [SODefault('none')]
    iconColor: string;
    [SODefault('none')]
    borderColor: string;
    [SODefault('none')]
    backgroundColor: string;
  end;

  TGuild = record

  [SODefault('0')]
    level: integer;
    [SODefault('0')]
    members: integer;
    [SODefault('0')]
    achievementPoints: integer;
    [SODefault('none')]
    name: string;
    [SODefault('none')]
    realm: string;
    [SODefault('none')]
    battlegroup: string;
    emblem: TEmblem;
  end;

  TCharacter = record

    achievementPoints: integer;
    lastModified: String;
    [SOName('class')]
    charclass: integer;
    race: integer;
    gender: integer;
    [SOName('level')]
    charlevel: integer;
    name: UTF8String;
    realm: UTF8String;
    battlegroup: string;
    thumbnail: string;
    [SODefault('none')]
    guild: TGuild;
  end;

  TRealm = record
    battlegroup: string;
    name: string;
    population: string;
    queue: boolean;
    slug: string;
    status: integer;
    [SOName('type')]
    Rtype: string;

  end;

  TArrayOfRealms = TArray<TRealm>;

  TBrokerResponse = class
  private
    FCharName: String;
    FCharRealm: String;
    FlastModified: TDateTime;
    FURL: string;
    FTimer: integer;
  public
    URLQueueObject: TURLQueueObject;
    property CharName: string read FCharName write FCharName;
    property CharRealm: string read FCharRealm write FCharRealm;
    property lastModified: TDateTime read FlastModified write FlastModified;
    property URL: string read FURL write FURL;
    property Timer: integer read FTimer write FTimer;
  end;

  TURLQueueObject = class
  private
    FCharName: String;
    FCharRealm: String;
    FURL: string;
    FlastModified: TDateTime;
    FPriority: integer;
    FStatus: integer;
    FRequesterId: String;
    FRequestTime: TDateTime;
    FRequestTry: integer;
    FRequestResult: string;
    FIsEmpty: boolean;
  public
    property URL: string read FURL write FURL;
    property CharName: string read FCharName write FCharName;
    property CharRealm: string read FCharRealm write FCharRealm;
    property lastModified: TDateTime read FlastModified write FlastModified;
    property Priority: integer read FPriority write FPriority;
    property status: integer read FStatus write FStatus;
    property RequesterId: string read FRequesterId write FRequesterId;
    property RequestTime: TDateTime read FRequestTime write FRequestTime;
    property RequestTry: integer read FRequestTry write FRequestTry;
    property RequestResult: string read FRequestResult write FRequestResult;
    property IsEmpty: boolean read FIsEmpty write FIsEmpty;
  end;

  TQueryArmoryResult = class(TBlobField)
  private
    FHTTPCode: integer;
    
    public
    property HTTPCode: Integer read FHTTPCode write FHTTPCode;

  end;

  TMSContainer = class
  private
    FNestedMS: TMemoryStream;
  public
    property NestedMS: TMemoryStream read FNestedMS write FNestedMS;
  end;

const

  Sp = ' ';

  // Queue status
  StatusPending = 0;
  StatusWorking = 1;
  StatusDone = 2;
  StatusHold = 3;
  StatusParsed = 4;
  StatusBadContent = 5;

  // WebRequester Status
  WebrequesterInitialized = 0;
  WebRequesterStarted = 1;
  WebRequesterThrottled = 2;
  WebRequesterStoped = 3;
  WebRequesterDisconnected = 4;
  WebRequesterQueueEmpty = 5;


  MaxRetriesPerRequester = 3;

  
function QueryArmory(URL: string): TQueryArmoryResult;

procedure logMessage(const msg: String);

implementation

function QueryArmory(URL: string): TQueryArmoryResult;
var
  MS: TMemoryStream;

  SO: ISuperObject;
  response: TQueryArmoryResult;
  IdHTTPClient: TIdHTTP;
  BF: TBlobField;
  st: string;
begin
  logMessage('begin of QueryArmory');
  MS := TMemoryStream.Create;
  BF := TBlobField.Create(nil);

  // response.RequestResult := TBlobField.Create(nil);
  IdHTTPClient := TIdHTTP.Create();
  try
    IdHTTPClient.get(
      URL,
      MS);
  except
    on E: EIdHTTPProtocolException do
    begin
      logMessage(E.ClassName + Sp + E.Message);
      // logMessage(E.ErrorMessage);
      response.HTTPCode := IdHTTPClient.responsecode;
      response.AsString := E.ErrorMessage;
    end;
  end;
  if IdHTTPClient.responsecode = 200 then
  begin

    
    
     if MS <> nil then
    response.LoadFromStream(MS);
    // SO := TSuperObject.ParseStream(MS, true);
    response.HTTPCode := IdHTTPClient.responsecode;
    // response.RequestResult.AsString := SO.AsJSon();
  end;
  MS.Free;
  IdHTTPClient.Free;
  Result := response;
end;




procedure logMessage(const msg: String);

var
  LogFileName, LogDirName: string;
  f: TextFile;
  msgData: string;
begin
  LogDirName := extractfilepath(Application.ExeName) + LOG_FILE_PATH;
  if not directoryexists(LogDirName) then
    forcedirectories(LogDirName);
  LogFileName := extractfilename(Application.ExeName);
  LogFileName := LogDirName + ChangeFileExt(LogFileName, '.log');
  AssignFile(
    f,
    LogFileName);

  if FileExists(LogFileName) then
    Append(f)
  else
    Rewrite(f);

  try
    msgData := Format(
      '%s : %s',
      [FormatDateTime('dd/mm/yyyy hh:mm:ss.zzz', Now), msg]);
    WriteLn(
      f,
      UTF8ToString(msgData));
  finally
    CloseFile(f)
  end;

end;

end.
